package pl.edu.pk.mech.kkbus.service;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.springframework.stereotype.Service;

import pl.edu.pk.mech.kkbus.component.MyEntityManagerFactory;
import pl.edu.pk.mech.kkbus.entities.Award;

@Service
public class AwardService {
    
    EntityManagerFactory emf = MyEntityManagerFactory.get();
    
    public List<pl.edu.pk.mech.kkbus.dto.Award> getAllAwards() {
        List<pl.edu.pk.mech.kkbus.dto.Award> result = 
                new LinkedList<pl.edu.pk.mech.kkbus.dto.Award>();
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Query query = em.createQuery("SELECT a FROM Award a ORDER BY a.name DESC");
            List<Award> results = (List<Award>) query.getResultList();
            Iterator<Award> iterator = results.iterator();
            while (iterator.hasNext()) {  
                Award award = iterator.next();
                
                result.add(new pl.edu.pk.mech.kkbus.dto.Award(award.getKey().getId(), 
                        award.getName(), award.getDescription(), award.getPoints(),
                        award.getQuantity()));
            }
            return result;         
        } catch(NoResultException e) {
            return null;
        } finally {
            if (em != null)
                em.close();
        }
    } 
    
    public boolean create(pl.edu.pk.mech.kkbus.dto.Award a) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            // Award
            Award award = new Award(a.getName(), a.getDescription(),
                    a.getPoints(), a.getQuantity());
            em.persist(award);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean update(long id, pl.edu.pk.mech.kkbus.dto.Award a) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Award award = em.find(Award.class, new Long(id));
            award.setDescription(a.getDescription());
            award.setName(a.getName());
            award.setPoints(a.getPoints());
            award.setQuantity(a.getQuantity());
            
            em.persist(award);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean delete(long id) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Award award = em.find(Award.class, new Long(id));            
            em.remove(award);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
}
